<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public function index()	{
		return $this->handler();
	}
	public function handler($url_main = "homepage", $get_func, $url_2, $url_3, $url_4, $url_5, $url_6, $url_7) {
		$url_main = urldecode($url_main);
		$get_func = urldecode($get_func);

		$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

		$page = $this->db->select()->from('content')->where('page_curl', $url_main)->get()->row_array();
		if (sizeof($page) <= 0) {
//			$test = $this->db->select()->from('catalog_items')->where('item_curl', $path)->where('item_status', 1)->get()->row_array();
//			if (sizeof($test) > 0) {
//				$page = $this->db->select()->from('content')->where('page_model', 'catalog')->where('page_function', 'index')->get()->row_array();
//				$url_1 = $path;
//				$get_func = "show";
//			} else {
//				$test = $this->db->select()->from('catalog_parents')->where('parent_curl', $url_main)->where('parent_status', 1)->get()->row_array();
//				if (sizeof($test) > 0) {
//					$page = $this->db->select()->from('content')->where('page_model', 'catalog')->where('page_function', 'index')->get()->row_array();
//					if (!$get_func) $url_1 = $url_main; else {
//
////							$test_item = $this->db->select()->from('catalog_items')->where('item_curl', $get_func)->get()->row_array();
////							if (sizeof($test_item) > 0) {
////								$url_1 = $get_func;
////								$get_func = 'show';
////							} else {
////								$url_1 = $get_func;
////								$get_func = "index";
////							}
//						}
//				} else {
					show_404(); exit();
//				}
//			}
		} else {
			if (preg_match("/^[0-9]+$/", $get_func)) {
				$url_1 = $get_func;
				$get_func = "show";
			} else {
				$test_news = $this->db->select()->from('news')->where('news_curl', $get_func)->get()->row_array();
				//dump($test_news);
				if (sizeof($test_news) > 0) {
					$url_1 = $get_func;
					$get_func = 'show';
				}
			}
		}


		$args[] = $url_1;
		$args[] = $url_2;
		$args[] = $url_3;
		$args[] = $url_4;
		$args[] = $url_5;
		$args[] = $url_6;
		$args[] = $url_7;

		if ($args[0] == "") array_shift($args);

		if ($page['page_model'] != "" && $page['page_function'] != "") {
			$model = $page['page_model']."_model";
			$function = $page['page_function'];
			if ($get_func != NULL) {
				if (in_array($get_func, $this->config->item('allowed_functions')))
				$function = $get_func;
				else { show_404(); exit(); }
			}
			$this->load->model('modules/'.$model);
			$from_model = $this->$model->$function($args);
			if (is_array($from_model)) {
				$data_from_model = $from_model['data'];
				$from_model = $from_model['text'];
			}
			//if ($model == "catalog_model" && $function == "show") $data['right_col_class'] = "no-right-col";
		} elseif ($page['page_model'] == "" && $page['page_function'] != "") {
			redirect($page['page_curl']);
		}
		if ($page['page_with_text'] == 1 && $function == $page['page_function']) {
			$data['out']['page_text'] = $page['page_text'];
		}
		$allpages = $this->db->select('page_title, page_curl, page_id, page_crumps')->from('content')->get()->result_array();
		foreach ($allpages as $allpages_one) {
			$book[$allpages_one['page_id']] = array('title' => $allpages_one['page_title'], 'curl' => $allpages_one['page_curl'], 'show' => $allpages_one['page_crumps']);
		}
		$cr = explode("&", $page['page_path']);
		$crumps = "<a href='/'>Главная</a>";
		foreach ($cr as $cr_one) {
			preg_match_all('#:[0-9]+:#', $cr_one, $num);
			$num = intval(str_replace(":", "", $num[0][0]));
			if ($num != 0 && $book[$num]['show'] == 1) {
				$crumps .= " &raquo; <a href='/".$book[$num]['curl']."/'>".$book[$num]['title']."</a>";
			}
		}
		if ($data_from_model['crumps'] != "") $crumps .= $data_from_model['crumps'];

		if ($this->uri->segment(1) != "") $data['crumps'] = $crumps;
		$data['out']['page'] = $page;
		$data['out']['page']['get_function'] = $function;
		$data['out']['from_model'] = $from_model;
		$data['out']['data_from_model'] = $data_from_model;
		if ($this->input->server('HTTP_X_REQUESTED_WITH') != 'XMLHttpRequest') {
			$this->layout->view('simple_page', $data);
		} else {
			if (is_array($data_from_model)) {
				echo $data['out']['data_from_model']['text']."<div class='close'></div>";
			} else {
				echo $data['out']['from_model']."<div class='close'></div>";
			}
		}
	}
	public function error_404() {
		include("./application/errors/error_404.php");
		exit();
	}
}